package com.cx.controller;


import com.cx.pojo.Hotel;
import com.cx.pojo.OrderForm;
import com.cx.pojo.Roominfo;
import com.cx.service.HotelService;
import com.cx.service.OrderFormService;
import com.cx.service.RoomService;
import com.cx.service.impl.CommentDao;
import com.cx.vo.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 订单接口
 */
@RestController
@RequestMapping("order")
public class OrderFormController {
    @Autowired
    HotelService hotelService;
    @Autowired
    RoomService roomService;
    @Autowired
    OrderFormService orderFormService;

    @Autowired
    private CommentDao commentDao;

    /**
     * 根据酒店id查酒店位置
     */
    @RequestMapping("selectHotelPositionByHotelId")
    @ResponseBody
    public ResultData selectHotelPositionByHotelId(Integer id) {
        ResultData resultData = new ResultData();

        Hotel hotel = orderFormService.selectHotelById(id);
        String hotelPosition = hotel.getHotelLocate();
        if (hotelPosition == null) {
            resultData.setCode(3);
            resultData.setMessage("数据为空");
        } else {
            resultData.setCode(0);
            resultData.setData(hotelPosition);
        }
        return resultData;
    }




    /**
     * 根据roomId和hotelId返回订单界面需要的信息
     */
    @RequestMapping("orderForm")
    @ResponseBody
    public ResultData selectRoomHotelById(Integer id, Integer roomId) {

        Hotel hotel = hotelService.selectHotelById(id);
        Roominfo room = roomService.selectRoomById(roomId);
        String hotelPosition = hotel.getHotelLocate();
//        String roomPicture = room.getroomPicture;
        List list = new ArrayList();
        list.add(hotel);
        list.add(room);
        list.add(hotelPosition);
//        list.add(roomPicture);

        ResultData resultData = new ResultData();
        if (null != hotel && null != room && null != hotelPosition) {
            resultData.setCode(1);
            resultData.setMessage("成功");
            resultData.setData(list);
        } else {
            resultData.setCode(2);
            resultData.setMessage("出错了");
        }
        return resultData;
    }




    /**
     * 生成表订单
     */
    @RequestMapping("produceorderform")
    @ResponseBody
    public ResultData OrderInfo(@RequestParam(value = "roomArrayList[]") Integer[] roomArrayList, HttpServletRequest request) throws ParseException {

        int resCode = 0;
        Integer[] arrayList = roomArrayList;
        long orderNo = (long) (System.currentTimeMillis() + Math.random() * 10000);
        for (int i = 0; i < Integer.valueOf(request.getParameter("roomCount")); i++) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            OrderForm orderForm = new OrderForm();
            // 到达时间
            orderForm.setOrderIndata(sdf.parse(request.getParameter("arriveData")));
            // 离开时间
            orderForm.setOrderOutdata(sdf.parse(request.getParameter("leaveData")));
            //预订数量
            orderForm.setOther1(Integer.valueOf(request.getParameter("roomCount")));
            //订单状态

            orderForm.setOrderStatus(Integer.parseInt(request.getParameter("orderFormStatus")));
            // 用户名字
            orderForm.setOrderName(request.getParameter("userName"));
//            // 用户手机号
//            orderForm.setUserPhoneNumber(request.getParameter("userPhoneNum"));
            // 用户id
            orderForm.setUserId(Integer.valueOf(request.getParameter("userId")));
            // 酒店id
            orderForm.setHotleId(Integer.valueOf(request.getParameter("hotelId")));
//            // 酒店名字
//            orderForm.setHotelName(request.getParameter("hotelName"));
//            //酒店位置
//            orderForm.setHotelPositon(request.getParameter("hotelPosition"));
//            // 酒店总价
//            orderForm.setOriginPrice(Long.valueOf(request.getParameter("originPrice")));
//            // 酒店单个现价
//            orderForm.setNowPrice(Long.valueOf(request.getParameter("nowPrice")));
//            // 酒店电话
//            orderForm.setHotelPhoneNumber(request.getParameter("hotelPhoneNumber"));
//            // 酒店房间名字
//            orderForm.setHotelRoomName(request.getParameter("hotelRoomName"));
//            // 房间设施
//            orderForm.setRoomService(request.getParameter("roomService"));
//            // 是否有早餐
//            if (request.getParameter("isBreakfast").equals("1")) {
//                orderForm.setIsBreakfast(true);
//            } else {
//                orderForm.setIsBreakfast(false);
//            }
//            // 是否有窗户
//            if (request.getParameter("isWindows").equals("1")) {
//                orderForm.setIsWindows(true);
//            } else {
//                orderForm.setIsWindows(false);
//            }
//            // 酒店优惠
//            orderForm.setDiscount(Long.valueOf(request.getParameter("discount")));
//
//            // 房间类型id
//            orderForm.setRoomId(Integer.parseInt(request.getParameter("roomId")));
//
//            // 酒店的城市位置
//            orderForm.setBigPosition(request.getParameter("bigPosition"));
//
            resCode = orderFormService.insertOrderForm(orderForm);
        }


        OrderForm orderForm = new OrderForm();
        // 酒店总价
        orderForm.setOrderAmount(Long.valueOf(request.getParameter("originPrice")));
//        // 房间设施
//        orderForm.setRoomService(request.getParameter("roomService"));
//        // 酒店房间名字
//        orderForm.setHotelRoomName(request.getParameter("hotelRoomName"));

        ResultData resultData = new ResultData();

        if (resCode >= 1) {
            resultData.setCode(1);
            resultData.setData(orderForm);
        } else {
            resultData.setCode(2);
        }

        return resultData;
    }


}
